﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;
using RssToolkit.Rss;
using System.Text.RegularExpressions;

namespace OpenQuarters.WebQuarters.Modules.ItemList
{
    public abstract class ItemListModuleBase<T> : Core.Module where T : class, IItemListable
    {
        private string itemType = typeof(T).FullName;

        [Newtonsoft.Json.JsonProperty]
        public string ItemType
        {
            get { return itemType; }
            set { itemType = value; }
        }

        private string summaryView = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, OptionSource = "OpenQuarters.WebQuarters.Modules.ItemList.ItemListModule.AvailableViews/[ItemType]")]
        public string Summary_View
        {
            get { return summaryView; }
            set { summaryView = value; }
        }

        private string mainView = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, OptionSource = "OpenQuarters.WebQuarters.Modules.ItemList.ItemListModule.AvailableViews/[ItemType]")]
        public string Main_View
        {
            get { return mainView; }
            set { mainView = value; }
        }

        private string pagerView = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, OptionSource = "OpenQuarters.WebQuarters.Modules.ItemList.ItemListModule.AvailableViews/[ItemType]")]
        public string Pager_View
        {
            get { return pagerView; }
            set { pagerView = value; }
        }

        private string fullViewPage = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.CMSPage)]
        public string Full_View_Page
        {
            get { return fullViewPage; }
            set { fullViewPage = value; }
        }

        [Newtonsoft.Json.JsonProperty]
        public List<string> FilterableFields
        {
            get
            {
                return Core.FilterableAttribute.GetFilterableFields(GeneralUtils.GetTypeByName(this.ItemType));
            }
        }

        [Newtonsoft.Json.JsonProperty]
        public List<string> OrderableFields
        {
            get
            {
                return Core.OrderableAttribute.GetOrderableFields(GeneralUtils.GetTypeByName(this.ItemType));
            }
        }

        private List<Filter> filters = new List<Filter>();
        [Newtonsoft.Json.JsonProperty]
        public List<Filter> Filters
        {
            get { return filters; }
            set { filters = value; }
        }

        private List<Ordering> ordering = new List<Ordering>();
        [Newtonsoft.Json.JsonProperty]
        public List<Ordering> Ordering
        {
            get { return ordering; }
            set { ordering = value; }
        }

        private bool maintainList = false;

        [PropertyConfiguration(PropertyType.CheckBox)]
        [Newtonsoft.Json.JsonProperty]
        public bool Maintain_List
        {
            get { return maintainList; }
            set { maintainList = value; }
        }

        private int itemsPerPage = 5;

        [PropertyConfiguration(PropertyType.TextBox)]
        [Newtonsoft.Json.JsonProperty]
        public int Items_Per_Page
        {
            get { return itemsPerPage; }
            set { itemsPerPage = value; }
        }

        private PaginationMode paginationMode = PaginationMode.Number_List;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown)]
        public PaginationMode Pagination_Mode
        {
            get { return paginationMode; }
            set { paginationMode = value; }
        }

        private PaginationPosition paginationPosition = PaginationPosition.Top;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown)]
        public PaginationPosition Pagination_Position
        {
            get { return paginationPosition; }
            set { paginationPosition = value; }
        }

        private bool showChildItems = false;

        [PropertyConfiguration(PropertyType.CheckBox)]
        [Newtonsoft.Json.JsonProperty]
        public bool Show_Child_Items
        {
            get { return showChildItems; }
            set { showChildItems = value; }
        }

        public override string GetJQueryEditorName()
        {
            return "cmsModuleEditor, cmsModuleEditor_ItemList";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ItemListModule ItemList;

            ItemList = new ItemListModule()
            {
                ItemType = typeof(T).FullName,
                Filters = filters,
                Ordering = ordering,
                Summary_View = summaryView,
                Main_View = mainView,
                Pager_View = pagerView,
                Full_View_Page = fullViewPage,
                Maintain_List = maintainList,
                ModuleId = this.ModuleId,
                Items_Per_Page = this.Items_Per_Page,
                Pagination_Mode = this.Pagination_Mode,
                Pagination_Position = this.Pagination_Position
            };

            this.Controls.Add(ItemList);
        }

        public virtual List<T> ChildItems()
        {
            if (this.showChildItems)
            {
                ItemLister<T> lister = new ItemLister<T>();

                lister.Configuration = new ItemListerConfiguration()
                {
                    Filters = filters,
                    Ordering = ordering,
                    SummaryView = summaryView,
                    MainView = mainView,
                    PaginationView = pagerView,
                    FullViewPage = fullViewPage,
                    MaintainList = maintainList,
                    Items_Per_Page = this.Items_Per_Page,
                    Pagination_Mode = this.Pagination_Mode,
                    Pagination_Position = this.Pagination_Position
                };

                return ((ItemListResponse)lister.GetType().GetMethod("BuildItemList").Invoke(lister, new object[] { })).Items.Cast<T>().ToList();
            }
            else
            {
                return new List<T>();
            }
        }

        public virtual T CurrentItem()
        {
            string url = Oracle.Request.Form["url"] != null ? Oracle.Request.Form["url"] : Oracle.Request.Url.ToString();
            if (!this.maintainList)
            {
                T urlItem = (T)Activator.CreateInstance<T>().GetItemFromUrl(url, new List<IItemListable>());
                if (urlItem != null)
                {
                    return urlItem;
                }
            }

            return null;
        }
    }
}
